import { generateColumns, generateDictData } from '@/utils/plusUtil.js'
import { listData, delData, getData, addData,  updateData } from "@/api/system/dict/data.js";
import { optionselect as getDictOptionselect } from '@/api/system/dict/type'

export const useCommonConfig = ()=>{

  // 基础配置
  const { proxy } = getCurrentInstance()
  const primaryKey = 'dictCode'
  const moduleName =  '字典数据'
  const apiFn = {
    listApiFn: listData,
    delApiFn: delData,
    apiFnConfig:{
      info: getData,
      add: addData,
      edit: updateData
    }
  }



  // 删除前处理
  const beforeDelFn = (data)=>{
    return Array.isArray(data) ? data.map((item) => item[primaryKey]).join([',']) : data[primaryKey]
  }

  return {
    proxy,
    primaryKey,
    moduleName,
    apiFn,
    beforeDelFn,
  }
}

/**
 * 获取列和字典
 * @param isForm
 * @param data  表单数据对象
 */
export const useColumnsDict = (isForm = false, data) => {
  const { proxy } = getCurrentInstance()
  let formData = ref({})
  if (isForm) {
    if (!data || !isRef(data)) {
      console.error('当 isForm 为 true 时，data 必须是一个 ref 对象')
      return
    }
    formData = data
  }

  const dictRawData  = proxy.useDict(...[
    'sys_normal_disable',
  ])

  /** 查询字典类型列表 */
  const typeOptions = ref([]);
  function getTypeList() {
    getDictOptionselect().then(response => {
      typeOptions.value = response.data;
    });
  }
  getTypeList()

  // 数据标签回显样式
  const listClassOptions = ref([
    {value: "default", label: "默认"},
    {value: "primary", label: "主要"},
    {value: "success", label: "成功"},
    {value: "info", label: "信息"},
    {value: "warning", label: "警告"},
    {value: "danger", label: "危险"}
  ]);

  // 拓展了propInTable，propInForm
  // 定义在这个里面可以对外层进行覆盖(根据是列表还是表单)
  // orderInForm 可以调整在表单里面显示顺序
  const columnsData = [
    {
      label: "字典类型",
      prop: "dictType",
      valueType: "select",
      options: typeOptions,
      optionsMap: {
        label: "dictType",
        value: "dictType"
      },
      hideInTable: true,
      tableColumnProps: {
        align: "center"
      },
      propInForm:{
       fieldProps:{
         disabled:true
       }
      }
    },
    {
      label: "字典编码",
      prop: "dictCode",
      hideInSearch: true,
      hideInForm: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "字典标签",
      prop: "dictLabel",
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "字典键值",
      prop: "dictValue",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "样式属性",
      prop: "cssClass",
      hideInSearch: true,
      hideInTable: true,
    },
    {
      label: "显示排序",
      prop: "dictSort",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      },
      propInForm:{
        valueType:"input-number",
      }
    },
    {
      label: "回显样式",
      prop: "listClass",
      hideInSearch: true,
      hideInTable: true,
      valueType: 'select',
      options: listClassOptions,
    },

    {
      label: "状态",
      prop: "status",
      valueType: "select",
      options: dictRawData.sys_normal_disable,
      tableColumnProps: {
        align: "center"
      },
      propInForm:{
        valueType:"radio"
      }
    },
    {
      label: "备注",
      prop: "remark",
      tableColumnProps: {
        align: "center"
      },
      propInForm:{
        valueType:"textarea"
      }
    },
    {
      label: "创建时间",
      prop: "createTime",
      hideInSearch: true,
      hideInForm: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "操作",
      prop: "action",
      width: 100,
      hideInSearch: true,
      hideInForm: true,
      tableColumnProps: {
        align: "center"
      }
    }
  ]
  const columns = ref([])
  columns.value = generateColumns(columnsData, isForm)
  console.log(columns)
  const dictData = computed(()=>generateDictData(dictRawData))
  return {
    dictData,
    columns,
  }
}

